package com.eshop.modules.activity.rest;

import org.springframework.web.bind.annotation.DeleteMapping;
import com.eshop.modules.aop.ForbidSubmit;
import java.io.Serializable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.validation.annotation.Validated;
import com.eshop.modules.activity.domain.StoreCouponIssue;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import io.swagger.annotations.ApiOperation;
import com.eshop.logging.aop.log.Log;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.data.domain.Pageable;
import com.eshop.modules.activity.service.dto.StoreCouponIssueQueryCriteria;
import com.eshop.modules.activity.service.StoreCouponIssueService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;

@Api(tags = { "商城:优惠券发布管理" })
@RestController
@RequestMapping({ "api" })
public class StoreCouponIssueController
{
    private final StoreCouponIssueService storeCouponIssueService;
    
    public StoreCouponIssueController(final StoreCouponIssueService storeCouponIssueService) {
        this.storeCouponIssueService = storeCouponIssueService;
    }
    
    @Log("查询已发布")
    @ApiOperation("查询已发布")
    @GetMapping({ "/yxStoreCouponIssue" })
    @PreAuthorize("@el.check('admin','YXSTORECOUPONISSUE_ALL','YXSTORECOUPONISSUE_SELECT')")
    public ResponseEntity getYxStoreCouponIssues(final StoreCouponIssueQueryCriteria criteria, final Pageable pageable) {
        return new ResponseEntity(this.storeCouponIssueService.queryAll(criteria, pageable), HttpStatus.OK);
    }
    
    @Log("发布")
    @ApiOperation("发布")
    @PostMapping({ "/yxStoreCouponIssue" })
    @PreAuthorize("@el.check('admin','YXSTORECOUPONISSUE_ALL','YXSTORECOUPONISSUE_CREATE')")
    public ResponseEntity create(@Validated @RequestBody final StoreCouponIssue resources) {
        if (resources.getTotalCount() > 0) {
            resources.setRemainCount(resources.getTotalCount());
        }
        return new ResponseEntity(this.storeCouponIssueService.save(resources), HttpStatus.CREATED);
    }
    
    @Log("修改状态")
    @ApiOperation("修改状态")
    @PutMapping({ "/yxStoreCouponIssue" })
    @PreAuthorize("@el.check('admin','YXSTORECOUPONISSUE_ALL','YXSTORECOUPONISSUE_EDIT')")
    public ResponseEntity update(@Validated @RequestBody final StoreCouponIssue resources) {
        this.storeCouponIssueService.saveOrUpdate(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }
    
    @ForbidSubmit
    @Log("删除")
    @ApiOperation("删除")
    @DeleteMapping({ "/yxStoreCouponIssue/{id}" })
    @PreAuthorize("@el.check('admin','YXSTORECOUPONISSUE_ALL','YXSTORECOUPONISSUE_DELETE')")
    public ResponseEntity delete(@PathVariable final Integer id) {
        this.storeCouponIssueService.removeById((Serializable)id);
        return new ResponseEntity(HttpStatus.OK);
    }
}
